﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Design.PropertyEditing;

namespace Okazuki.BehaviorSupport.Design.Commons.Editors
{
    /// <summary>
    /// EventNamePropertyEditorView.xaml の相互作用ロジック
    /// </summary>
    public partial class EventNamePropertyEditorView : UserControl
    {
        public EventNamePropertyEditorView()
        {
            InitializeComponent();
            SetBinding();
        }

        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var oldPropertyValue = e.OldValue as PropertyValue;
            if (oldPropertyValue != null)
            {
                var oldContext = oldPropertyValue.ParentProperty.ModelProperties.First().Parent;
                oldContext.PropertyChanged -= eventTriggerModel_PropertyChanged;
            }

            var propertyValue = e.NewValue as PropertyValue;
            if (propertyValue == null)
            {
                return;
            }

            var newContext = propertyValue.ParentProperty.ModelProperties.First().Parent;
            newContext.PropertyChanged += eventTriggerModel_PropertyChanged;

            RefleshEventNames(propertyValue);
            comboBoxEventNames.Text = propertyValue.StringValue;
        }

        private void RefleshEventNames(PropertyValue propertyValue)
        {
            if (propertyValue == null)
            {
                return;
            }

            var modelProperty = propertyValue.ParentProperty.ModelProperties.First();
            var eventTriggerModel = modelProperty.Parent;
            if (eventTriggerModel.Properties["SourceName"].IsSet || eventTriggerModel.Properties["SourceObject"].IsSet)
            {
                ClearBinding();
                comboBoxEventNames.ItemsSource = null;
                SetBinding();
                return;
            }

            var eventTriggerParent = eventTriggerModel.Parent;
            comboBoxEventNames.ItemsSource = eventTriggerParent.Events.Select(
                evt => evt.Name);
        }

        private void eventTriggerModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName != "SourceObject" && e.PropertyName != "SourceName")
            {
                return;
            }
            RefleshEventNames(this.DataContext as PropertyValue);
        }

        private void SetBinding()
        {
            var b = new Binding("Value");
            comboBoxEventNames.SetBinding(
                ComboBox.TextProperty, b);
        }

        private void ClearBinding()
        {
            BindingOperations.ClearAllBindings(comboBoxEventNames);
        }
    }
}
